package com.facesteel.speech_play.utils

import android.annotation.SuppressLint
import android.app.Service.POWER_SERVICE
import android.content.Context
import android.os.PowerManager
import android.util.Log

class ScreenUtil private constructor() {

    companion object {
        private var powerManager: PowerManager? = null
        private var isOff: Boolean = false
        private var wakeLockScreen: PowerManager.WakeLock? = null

        @SuppressLint("InvalidWakeLockTag")
        fun setScreenOff(context: Context) {
            if(powerManager == null){
                powerManager = context.getSystemService(POWER_SERVICE) as PowerManager?
            }

            powerManager?.let {
                if (wakeLockScreen == null){
                    wakeLockScreen = it.newWakeLock(32, "SCREEN:LOCK")
                }

                wakeLockScreen?.let { screenLock ->
                    Log.i("当前屏幕状态", "" + screenLock.isHeld)
                    if (screenLock.isHeld) {
                        return
                    } else {
                        screenLock.acquire(10*60*1000L /*10 minutes*/) // 申请设备电源锁
                        isOff = true
                    }
                }

            }

        }

        fun setScreenOn() {
            if (isOff) {
                wakeLockScreen?.let {
                    if (it.isHeld) {
                        it.setReferenceCounted(false)
                        it.release() // 释放设备电源锁
                    }
                }
                isOff = false
            }
        }
    }
}